package com.czl.module_base.utils;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.czl.module_base.R;

/**
 * @author by xubin,
 * @date on 2022/2/10.
 * PS: Not easy to write code, please indicate.
 */
public class GlideUtils {
    public static void loadImage(Context context, ImageView imageView, @DrawableRes int resourceId, String imageUrl) {
        RequestOptions options = new RequestOptions()
                .centerInside()
                //预加载图片
                .placeholder(resourceId)
                //加载失败显示图片
                .error(resourceId)
                //优先级
                .priority(Priority.HIGH)
                //缓存策略
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(context).load(imageUrl).apply(options).into(imageView);
    }

    public static void loadImage(Context context, ImageView imageView, @DrawableRes int resourceId, Uri uri) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                //预加载图片
                .placeholder(resourceId)
                //加载失败显示图片
                .error(resourceId)
                //优先级
                .priority(Priority.HIGH)
                //缓存策略
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(context).load(uri).apply(options).into(imageView);
    }

    public static void loadImage(Context context, ImageView imageView, String imageUrl, @DrawableRes int resourceId, @DrawableRes int errorRes) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                //预加载图片
                .placeholder(resourceId)
                //加载失败显示图片
                .error(errorRes)
                //优先级
                .priority(Priority.HIGH)
                //缓存策略
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(context).load(imageUrl).apply(options).into(imageView);
    }

    public static void initImageWithFileCache(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .centerCrop()
                .into(imageView);
    }

    public static void initImageNoCache(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .skipMemoryCache(true)
                .dontAnimate()
                .centerCrop()
                .into(imageView);
    }
}
